from app.outdoor.models import ActivityTypeTemplate, ActivityType
from common.authCheck.userAuth import check_login, getUser
from common.dictInfo import model_to_dict
from rest_framework.views import APIView
from django.http import JsonResponse


class ActivityTypeTemplatesInfoView(APIView):

    @check_login
    def get(self, request, typeID):
        """
        根据活动来获取数据库中的模板信息
        :param request:
        :param typeID:
        :return:
        """
        activity_type = ActivityType.objects.filter(id=typeID)
        if not activity_type.exists():
            return JsonResponse({
                'status': False,
                'errMsg': '类型不存在'
            }, status=404)
        activity_type = activity_type[0]
        templates = ActivityTypeTemplate.objects.filter(related_activity_type=activity_type)
        if not templates.exists():
            return JsonResponse({
                'status': True,
                'message': '该分类无模板'
            }, status=404)
        templates = templates[0]
        templates_dict = model_to_dict(templates, exclude='related_activity_type')
        return JsonResponse({
            'status': True,
            'templates': templates_dict
        })
